
  /* UJIVE */





use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"


keep if time==1


egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)


tab obs_lawyer
tab n_lawyer

keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



keep if n_judge>1

* Estimate first stage using instrument dummies. Include covariates and instrument dummies in first step extract without dummies. 

local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



tab lawyer_id_unique, gen(lawyer_dum)

gen treated_cov=.
gen treated_dummies_cov=.

egen court_pyear =group(court pyear) 
tab court_pyear

* Leave out own obs
sort court_pyear lawyer_id_unique
gen n=_n
gen index_court=n[_n+1] if court_pyear==court_pyear[_n+1]
replace index_court=n[_n-1] if court_pyear==court_pyear[_n-1] & index_court==.

sort lawyer_id_unique
gen index_lawyer=n[_n+1] if lawyer_id_unique==lawyer_id_unique[_n+1]
replace index_lawyer=n[_n-1] if lawyer_id_unique==lawyer_id_unique[_n-1] & index_lawyer==.
sort n
gen loop_number=.
forvalues j = 1(1)`=_N' {


reghdfe granted male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1 if n!=`j',  absorb(court_pyear_fe=court_pyear, savefe)
predict treat_hat_cov if n==`j', xb
replace treated_cov=treat_hat_cov+court_pyear_fe[index_court] if n==`j'
drop court_pyear_fe treat_hat_cov

reghdfe granted male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1 if n!=`j',  absorb(court_pyear_fe=court_pyear lawyer_id_fe=lawyer_id_unique, savefe)
predict treat_hat_cov_dum if n==`j', xb
replace treated_dummies_cov=treat_hat_cov_dum+court_pyear_fe[index_court]+lawyer_id_fe[index_lawyer] if n==`j'
drop court_pyear_fe lawyer_id_fe treat_hat_cov_dum
replace loop_number=`j'
}
gen z_ujive=treated_dummies_cov-treated_cov

corr z_ujive iv_lawyer2

reg granted z_ujive
reg granted iv_lawyer2


keep pnr iv_lawyer2 lawyer_id_unique z_ujive

cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Data
save IV_ujive, replace

use "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Data\IV_ujive", clear 

merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"




keep if _merge==3
drop _merge

keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab time

tab aar, gen(aardum)




local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"


cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"

/* IV Regress */


// Complier mean "Family welfare culture style" NEW




ivreghdfe erhvervsindk_real (granted=z_ujive) `Xvar_short' aardum2-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_ujive.xls, replace ctitle(UJIVE erhvervsindk_real) keep(granted) 



local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house mortgage_real bankgaeld"





foreach var of varlist `depvar'{

// complier means for non-granted




ivreghdfe `var' (granted=z_ujive) `Xvar_short' aardum2-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_ujive.xls, append ctitle(UJIVE `var')  keep(granted) 




}





use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 

cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"
keep if time==1


egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court*obs_court-granted)/(obs_court-1)

* Data restrictions more than 10 obs pr lawyer and multiple lawyers in a given court*year

/*
keep if age0>29 & age0<51
*/

tab obs_lawyer
tab n_lawyer

keep if n_lawyer>=50

egen n_judge=nvals(lawyer_id_unique), by(court aar)



keep if n_judge>1

* Estimate first stage using instrument dummies. Include covariates and instrument dummies in first step extract without dummies. 

local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



tab lawyer_id_unique, gen(lawyer_dum)

gen treated_cov=.
gen treated_dummies_cov=.

egen court_pyear =group(court pyear) 
tab court_pyear

* Leave out own obs
sort court_pyear lawyer_id_unique
gen n=_n
gen index_court=n[_n+1] if court_pyear==court_pyear[_n+1]
replace index_court=n[_n-1] if court_pyear==court_pyear[_n-1] & index_court==.

sort lawyer_id_unique
gen index_lawyer=n[_n+1] if lawyer_id_unique==lawyer_id_unique[_n+1]
replace index_lawyer=n[_n-1] if lawyer_id_unique==lawyer_id_unique[_n-1] & index_lawyer==.
sort n
gen loop_number=.
forvalues j = 1(1)`=_N' {


reghdfe granted male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1 if n!=`j',  absorb(court_pyear_fe=court_pyear, savefe)
predict treat_hat_cov if n==`j', xb
replace treated_cov=treat_hat_cov+court_pyear_fe[index_court] if n==`j'
drop court_pyear_fe treat_hat_cov

reghdfe granted male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_mean aktiv_mean higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1 if n!=`j',  absorb(court_pyear_fe=court_pyear lawyer_id_fe=lawyer_id_unique, savefe)
predict treat_hat_cov_dum if n==`j', xb
replace treated_dummies_cov=treat_hat_cov_dum+court_pyear_fe[index_court]+lawyer_id_fe[index_lawyer] if n==`j'
drop court_pyear_fe lawyer_id_fe treat_hat_cov_dum
replace loop_number=`j'
}
gen z_ujive=treated_dummies_cov-treated_cov

corr z_ujive iv_lawyer2

reg granted z_ujive
reg granted iv_lawyer2


keep pnr iv_lawyer2 lawyer_id_unique z_ujive

cd G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Data
save IV_ujive_50, replace

use "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Data\IV_ujive_50", clear 

merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"




keep if _merge==3
drop _merge

keep if time>0

tab lawyer_id_unique, gen(lawyer_dum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab time

tab aar, gen(aardum)



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"


cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"

/* IV Regress */


// Complier mean "Family welfare culture style" NEW



ivreghdfe erhvervsindk_real (granted=z_ujive) `Xvar_short' aardum2-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_ujive_50.xls, replace ctitle(UJIVE erhvervsindk_real) keep(granted) 



local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted




ivreghdfe `var' (granted=z_ujive) `Xvar_short' aardum2-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_ujive_50.xls, append ctitle(UJIVE `var')  keep(granted) 




}


local depvar " bankgaeld"





foreach var of varlist `depvar'{

// complier means for non-granted




ivreghdfe `var' (granted=z_ujive) `Xvar_short' aardum5-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_ujive_50.xls, append ctitle(UJIVE `var')  keep(granted) 




}